package com.yogee.topseller.fragment.zhoubian;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.yogee.core.base.HttpFragment1;
import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.core.utils.ToastUtils;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.ZhoubianActivity;
import com.yogee.topseller.adapter.ZxAdapter;
import com.yogee.topseller.bean.JigouBean;
import com.yogee.topseller.bean.XwzxBean;
import com.yogee.topseller.fragment.productdetail.BaseFragment;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.utils.AppUtil;
import com.yogee.topseller.utils.LogUtils;
import com.yogee.topseller.view.EmptyRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 风险--资讯
 * Created by zwj on 2017/12/7.
 */

public class ZixunFragment extends HttpFragment1 implements ZhoubianActivity.UpdateListener{

    @BindView(R.id.rv_fengxian)
    EmptyRecyclerView rv_fengxian;
    @BindView(R.id.id_empty_view)
    View id_empty_view;

    ZxAdapter xzcfItemAdapter;
    List<XwzxBean.Wenjian> list1 = new ArrayList<>();
    Unbinder unbinder1;
    private int isMj = 0;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_fengxian;
    }


    @Override
    protected void initView() {
//        ((ProductDetailsActivity)getActivity()).setListener(this);
//        beans = (ProductInfoBean) getArguments().getSerializable("baseInfo");
//        list = beans.getInterest();
        ((ZhoubianActivity)getActivity()).setListener2(this);
        list1.clear();
//        XwzxBean.Wenjian wenjian = new XwzxBean.Wenjian();
//        list1.add(wenjian);
//        list1.add(wenjian);
//        list1.add(wenjian);
        rv_fengxian.setLayoutManager(new LinearLayoutManager(getActivity()));

        xzcfItemAdapter = new ZxAdapter(getActivity());
        rv_fengxian.setAdapter(xzcfItemAdapter);
        rv_fengxian.setEmptyView(id_empty_view);
//        xzcfItemAdapter.addData(list1);
        getList(((ZhoubianActivity)getActivity()).getMySelectCompany().getId(),((ZhoubianActivity)getActivity()).getMySelectCompany().getType(),((ZhoubianActivity)getActivity()).getMySelectCompany().getProId());
    }


    private void getList(String relatedCompanyId,String type,String proId){
        if(TextUtils.isEmpty(relatedCompanyId))
            return;
        if(TextUtils.isEmpty(proId))
            return;
        HttpManager.getInstance().eyeCheckXw(relatedCompanyId,"XWLB", AppUtil.getUserId(getActivity()),type,proId).compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<XwzxBean>() {
            @Override
            public void onNext(XwzxBean o) {
//                list1.addAll(o.getMechanismList());
                if(o.getEyeCheck().getError_code()==0)
                    xzcfItemAdapter.addData(o.getEyeCheck().getResult());
                else
                    xzcfItemAdapter.addData(null);

            }
        }, this));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder1 = ButterKnife.bind(this, rootView);

        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder1.unbind();
    }

    @Override
    public void getCurrentPos(JigouBean.Company company) {

        if(company!=null){
            LogUtils.e("sfz","资讯:"+company.getName());
            LogUtils.e("sfz","资讯产品id:"+company.getProId());
            getList(company.getId(),company.getType(),company.getProId());
        }

        else{
            xzcfItemAdapter.addData(null);
        }
    }
}
